<!-- 发表新的主题帖  -->

<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.PreparedStatement" %>
<%@page import="java.sql.Statement" %>
<%@page import="java.sql.ResultSet" %>
<%@page import="com.sxt.bbs.db.DB" %>

<%
	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");
	String action = request.getParameter("post");

	if (action != null && action.trim().equals("action")) {
		String title = request.getParameter("title");
		String cont = request.getParameter("cont");

		Connection conn = DB.getConn();
		String sql = "insert into article values (null, ?, ?, ?, ?, now(), ?)";

		boolean autoCommit = conn.getAutoCommit();
		conn.setAutoCommit(false);

		int rootId = -1;

		PreparedStatement pStat = DB.createPStat(conn, sql, Statement.RETURN_GENERATED_KEYS);
		pStat.setInt(1, 0);
		pStat.setInt(2, rootId);
		pStat.setString(3, title);
		pStat.setString(4, cont);
		pStat.setInt(5, 0);
		pStat.executeUpdate();

		ResultSet rsKey = pStat.getGeneratedKeys();
		rsKey.next();
		rootId = rsKey.getInt(1);
		
		Statement stat = DB.createStat(conn);
		stat.executeUpdate("update article set rootid = " + rootId + " where id = " + rootId);

		conn.commit();
		conn.setAutoCommit(autoCommit);

		DB.close(stat);
		DB.close(rsKey);
		DB.close(pStat);
		DB.close(conn);
		
		response.sendRedirect("article.jsp");
	}
%>

<!DOCTYPE html>
<!--
Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.md or http://ckeditor.com/license
-->
<html>
<head>
<meta charset="utf-8">
<title>Replace Textareas by Class Name &mdash; CKEditor Sample</title>
<script src="ckeditor/ckeditor.js"></script>
<link rel="stylesheet" href="ckeditor/samples/sample.css">
</head>
<body>
	<form action="post.jsp" method="post">
		<input type="hidden" name="post" value="action" /> 
		<p>
			<label> 请输入标题： </label> <input type="text" name="title" /><br>

			<label for="editor1"> 请输入回复内容： </label>
			<textarea class="ckeditor" cols="80" id="editor1" name="cont"
				rows="10"></textarea>
		</p>
		<p>
			<input type="submit" value="Submit">
		</p>
	</form>
	<div id="footer">
		<hr>
		<p>
			CKEditor - The text editor for the Internet - <a class="samples"
				href="http://ckeditor.com/">http://ckeditor.com</a>
		</p>
		<p id="copy">
			Copyright &copy; 2003-2015, <a class="samples"
				href="http://cksource.com/">CKSource</a> - Frederico Knabben. All
			rights reserved.
		</p>
	</div>
</body>
</html>
